package com.cdtu.controller;

import com.cdtu.entity.SysDept;
import com.cdtu.entity.SysEmployee;
import com.cdtu.entity.SysExam;
import com.cdtu.entity.SysRole;
import com.cdtu.service.SysExamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;


@RestController
@RequestMapping("/exam")
//解决跨域问题
@CrossOrigin
public class SysExamContrller extends BaseController {
    @Autowired
    private SysExamService examService;

    //列表
    @RequestMapping("/list")
    public ModelAndView examList(ModelAndView modelAndView) {
        //调用业务
        List<SysExam> list = examService.list();
        //返回数据
        modelAndView.addObject("sysexamList", list);
        //跳转页面
        modelAndView.setViewName("system/sysExam/examList");

        return modelAndView;
    }
    //删除
    @RequestMapping("/delete/{examId}")
    public ModelAndView delete(@PathVariable("examId") long examId, ModelAndView modelAndView) {
        //调用业务
        boolean remove = examService.removeById(examId);
        //跳转页面
        modelAndView.setViewName("redirect:/exam/list");

        return modelAndView;
    }
    //添加
    @RequestMapping("/addExam")
    public ModelAndView addExamUI(SysExam sysExam, ModelAndView modelAndView) {
        //调用业务
        examService.save(sysExam);
        //2.跳转页面;
        modelAndView.setViewName("redirect:/exam/list");

        return modelAndView;
    }
    //添加UI
    @RequestMapping("/toAddUI")
    public ModelAndView toAddSysExam(ModelAndView modelAndView) {
        //2.跳转页面;
        modelAndView.setViewName("system/sysExam/addSysExam");

        return modelAndView;
    }

    //修改UI
    @RequestMapping("/toUpdateUI/{examId}")
    public ModelAndView toUpdateUi(ModelAndView modelAndView, @PathVariable("examId") Long examId) {
        //调用业务
        SysExam sysExam = examService.getById(examId);

        //返回数据
        modelAndView.addObject("sysExam", sysExam);

        //2.跳转页面;
        modelAndView.setViewName("system/sysExam/updateSysExam");
        return modelAndView;
    }
    //修改
    @RequestMapping("/update")
    public ModelAndView update(ModelAndView modelAndView, SysExam sysExam) {
        //调用业务
        examService.updateById(sysExam);
        //2.跳转页面;
        modelAndView.setViewName("redirect:/exam/list");
        return modelAndView;
    }
}
